local jd = require "packages/Super_Skeld/jd"
local jd_test = require "packages/Super_Skeld/jd_test"
local old = require "packages/Super_Skeld/old"

local U = require "packages/utility/utility"

local jdgm = Package:new("Skeldgamemode", Package.SpecialPack)
jdgm:addGameMode(require "packages/Super_Skeld/1v2Skeld")

local jd_cards = Package:new("jd_cards", Package.CardPack)
jd_cards.extensionName = "jd_cards"


local piaoAudio = fk.CreateTriggerSkill{
  name = "#piao_Audio",
  refresh_events = {fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and player.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:broadcastPlaySound("./packages/Super_Skeld/audio/card/piao")
    room:sendLog{
      type = "#InvokeSkill",
      from = player.id,
      arg = "piao",
    }
  end,
}
local piaoSkill = fk.CreateActiveSkill{
  name = "piao_Skill",
  prompt = "#piaoSkill",
  can_use = Util.CanUse,
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, user, card)
    return user ~= to_select
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room,effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.to)
    if to:getMark("@@piao_kick_1_3") == 1 then
      room:setPlayerMark(to,"@@piao_kick_1_3",0)
      room:setPlayerMark(to,"@@piao_kick_2_3",1)
    elseif to:getMark("@@piao_kick_2_3") == 1 then
      room:setPlayerMark(to,"@@piao_kick_2_3",0)
      room:setPlayerMark(to,"@@piao_kick_3_3",1)
      to:turnOver()
      to:throwAllCards("h")
      room:setPlayerMark(to,"@@piao_kick_3_3",0)
    else
      room:setPlayerMark(to,"@@piao_kick_1_3",1)
    end
  end,
}
piaoSkill:addRelatedSkill(piaoAudio)
Fk:addSkill(piaoSkill)
local piao = fk.CreateTrickCard{
  name = "piao",
  number = 7,
  suit = Card.Heart,
  skill = piaoSkill,
  special_skills = { "recast" },
}
jd_cards:addCards{
  piao,
  piao:clone(Card.Diamond, 9),
  piao:clone(Card.Spade, 2),
  piao:clone(Card.Club, 13),
  piao:clone(Card.Heart, 1),
  piao:clone(Card.Heart, 2),
}


local yeniuAudio = fk.CreateTriggerSkill{
  name = "#yeniuAudio",
  refresh_events = {fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and
      data.card.trueName == "slash"
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:broadcastPlaySound("./packages/Super_Skeld/audio/card/cha_bizon")
    room:sendLog{
      type = "#InvokeSkill",
      from = player.id,
      arg = "yeniu",
    }
  end,
}
local yeniuSkill = fk.CreateTargetModSkill{
  name = "#yeniu_skill",
  attached_equip = "yeniu",
  frequency = Skill.Compulsory,
  residue_func = function(self, player, skill, scope, card)
    if player:hasSkill(self) and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      --FIXME: 无法检测到非转化的cost选牌的情况，如活墨等
      local cardIds = Card:getIdList(card)
      local yenius = table.filter(player:getEquipments(Card.SubtypeWeapon), function(id)
        return Fk:getCardById(id).equip_skill == self
      end)
      if #yenius == 0 or not table.every(yenius, function(id)
        return table.contains(cardIds, id)
      end) then
        return 2
      end
    end
  end,
}
local yeniuSkill_2= fk.CreateTriggerSkill{
  name = "#yeniuSkill_2",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.card.trueName == "slash" and not data.chain and data.damage > 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.damage = 1
    player:broadcastSkillInvoke(self.name, 2)
    room:notifySkillInvoked(player, self.name, "negative")
  end,
}
yeniuSkill:addRelatedSkill(yeniuSkill_2)
yeniuSkill:addRelatedSkill(yeniuAudio)
Fk:addSkill(yeniuSkill)

local yeniu = fk.CreateWeapon{
  name = "yeniu",
  suit = Card.Spade,
  number = 10,
  attack_range = 7,
  equip_skill = yeniuSkill,
}

jd_cards:addCard(yeniu)

Fk:loadTranslationTable{
  ["jd_cards"] = "SKELD卡牌",
  ["piao"] = "票",
  ["@@piao_kick_1_3"] = '<font color="red">■■</font>□□□□',
  ["@@piao_kick_2_3"] = '<font color="red">■■■■</font>□□',
  ["@@piao_kick_3_3"] = '<font color="red">■■■■■■</font>',
  ["#piao_kick"] = '<font color="red">票',
  ["#piaoSkill"] = "三票踢人，无需多言！",
  [":piao"] = "锦囊牌<br />" ..
    "<b>时机</b>：出牌阶段<br />" ..
    "<b>目标</b>：一名其他角色<br />" ..
    "<b>效果</b>：将此牌置于其武将牌上，若达到三票则其翻面并弃置所有手牌。",


  ["yeniu"] = "野牛",
  [":yeniu"] = "装备牌·武器<br /><b>攻击范围</b>：7<br /><b>武器技能</b>：锁定技，你于出牌阶段内使用【杀】次数加2，使用杀时只能造成1点伤害。",
  ["#yeniuSkill_2"] = "野牛",
}



return {
  jd,
  jd_cards,
  jdgm,
  jd_test,
  old,
}